import Vue from 'vue'
import Vuex from 'vuex'
import Util from '@/common/js/util'
import Config from '@/common/js/config'
import Api from '../api/index'

Vue.use(Vuex)

console.log(`是否开启Vuex严格模式：${process.env.NODE_ENV !== 'production'}`);
const store = new Vuex.Store({
	strict: process.env.NODE_ENV !== 'production',

	state: {
		userInfo: {},
		shopInfo: {},
		salesTicketGoodsList: []
	},

	getters: {},

	mutations: {
		updateUserInfo(state, payload) {
			// payload.growth = 1299
			// payload.vipExpireTime = '2023-11-2 15:30:00'
			state.userInfo = payload
		},
		updateShopInfo(state, payload) {
			state.shopInfo = payload
		},
		updateSalesTicketGoodsList(state, payload) {
			state.salesTicketGoodsList = payload
		}
	},

	actions: {
		updateUserInfo({
			dispatch,
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				Api.subscriber().then(async ({
					data
				}) => {
					commit('updateUserInfo', data)
					await dispatch('updateShopInfo')
					resolve(data)
				})
			})
		},
		updateShopInfo({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				Api.shopBrief().then(({
					data
				}) => {
					if (data) {
						console.log(data);
						data.telephone = data.telephone
						data.license = Util.concatRequestUrl(data.license)
						data.logoImg = Util.concatRequestUrl(data.logoImg)
						data.qrCode = Util.concatRequestUrl(data.qrCode)
						data.cardReverse = Util.concatRequestUrl(data.cardReverse)
						data.cardFront = Util.concatRequestUrl(data.cardFront)
						data.juridicalPersonPhoto = Util.concatRequestUrl(data
							.juridicalPersonPhoto)
						if (data.showImages) {
							data.showImages = data.showImages.split(',')
							data.showImages = Util.concatRequestUrl(data.showImages)
						}
						uni.setStorageSync('shopInfo', data)
						
						// let option = {
						// 	nickname: data.name,
						// 	avatarurl: Util.concatRequestUrl(data.logoImg),
						// 	mail: "123@qq.com",
						// 	phone: data.telephone,
						// 	gender: "mall",
						// 	birth: "",
						// 	sign: "商家",
						// 	ext: JSON.stringify({
						// 		nationality: "China",
						// 		merit: "Hello, world！",
						// 	}),
						// };
						// uni.WebIM.conn.updateUserInfo(option).then((res) => {
						// 	console.log('设置环信用户属性成功',res);
						// });
						
						
						commit('updateShopInfo', data)
						resolve(data)
					}
				})
			})
		}
	}
})

export default store